package cn.ivicar.oss.util;

import cn.com.carsmart.ws.model.Customer;
import cn.com.carsmart.ws.model.SimInfo;
import cn.com.carsmart.ws.model.Vehicle;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;

/**
 * Created by Administrator on 2017/5/16.
 */
public class JsonUtils {


    public static JSONObject getVehicleJSON(Vehicle vehicle) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("vid", vehicle.getId());
        jsonObject.put("name", StringUtils.isNotBlank(vehicle.getName()) ? vehicle.getName() : "");
        jsonObject
            .put("serial", StringUtils.isNotBlank(vehicle.getSerial()) ? vehicle.getSerial() : "");
        jsonObject.put("plate",
            StringUtils.isNotBlank(vehicle.getLicensePlate()) ? vehicle.getLicensePlate() : "");
        jsonObject.put("vin", StringUtils.isNotBlank(vehicle.getVin()) ? vehicle.getVin() : "");
        jsonObject.put("ein", StringUtils.isNotBlank(vehicle.getEin()) ? vehicle.getEin() : "");
        jsonObject.put("created", sdf.format(vehicle.getCreated()));

        return jsonObject;
    }

    public static JSONObject getCustomerJSON(Customer customer) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", customer.getId());
        jsonObject.put("mobile", StringUtils.isNotBlank(customer.getMobile()) ? customer.getMobile() : "");
        jsonObject.put("email", StringUtils.isNotBlank(customer.getEmail()) ? customer.getEmail() : "");
        jsonObject.put("nickName", StringUtils.isNotBlank(customer.getNickname()) ? customer.getNickname() : "");
        jsonObject.put("sex", (StringUtils.isBlank(customer.getSex())) ?
            "未知" :
            ((Integer.parseInt(customer.getSex()) == 1) ? "男" : "女"));
        jsonObject.put("birthday", (customer.getBirthday() != null) ? (sdf.format(customer.getBirthday())) : "");
        jsonObject.put("created", (customer.getCreated() != null) ? (sdf.format(customer.getCreated())) : "");
        return jsonObject;
    }

    public static JSONObject getSimInfoJSON(SimInfo simInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("iccid", StringUtils.isNotBlank(simInfo.getIccid()) ? simInfo.getIccid() : "");
        jsonObject.put("userCode", StringUtils.isNotBlank(simInfo.getIccid()) ? simInfo.getUserCode() : "");
        String operator = "未知";
        if (simInfo.getOperator() != null) {
            switch (simInfo.getOperator()) {
                case 1:
                    operator = "移动";
                    break;
                case 2:
                    operator = "电信";
                    break;
                case 3:
                    operator = "联通";
                    break;
            }
        }

        jsonObject.put("operator", operator);
        //卡状态
        String carStatus = "未知";
        if (simInfo.getCarStatus() != null) {
            switch (simInfo.getCarStatus()) {
                case "00":
                    carStatus = "正常";
                    break;
                case "01":
                    carStatus = "未知";
                    break;
                case "02":
                    carStatus = "停机";
                    break;
                case "03":
                    carStatus = "待激活";
                    break;
                case "04":
                    carStatus = "销号";
                    break;
            }
        }
        jsonObject.put("carStatus", carStatus);

        //switchStatus设备状态 0：关机 1：开机  2：未知
        String switchStatus = "未知";
        if (simInfo.getSwitchStatus() != null) {
            switch (simInfo.getSwitchStatus()) {
                case "0":
                    switchStatus = "关机";
                    break;
                case "1":
                    switchStatus = "开机";
                    break;
                case "2":
                    switchStatus = "未知";
                    break;
            }
        }
        jsonObject.put("switchStatus", switchStatus);

        String GPRS = "未知";
        if (simInfo.getGprsStatus() != null) {
            switch (simInfo.getGprsStatus()) {
                case "00":
                    GPRS = "离线";
                    break;
                case "01":
                    GPRS = "在线";
                    break;
            }
        }
        jsonObject.put("GPRS",GPRS);//GPRS状态  （00：离线  01：在线  02：未知）

        String isPauseGprs = "未知";
        if (simInfo.getIsPauseGprs() != null) {
            switch (simInfo.getIsPauseGprs()) {
                case "00":
                    isPauseGprs = "暂停";
                    break;
                case "01":
                    isPauseGprs = "正常";
                    break;
            }
        }
        jsonObject.put("flowOnline",isPauseGprs);//流量在线状态

        jsonObject.put("userAff","鑫洋泉电子科技有限公司");//归属客户
        //jsonObject.put("effectTime", simInfo.getEffectTime() != null ? (sdf.format(simInfo.getEffectTime())) : "");
        //jsonObject.put("orgId", (simInfo.getOrgId() != null) ? simInfo.getOrgId() : 0);
        //jsonObject.put("created", simInfo.getCreated() != null ? (sdf.format(simInfo.getCreated())) : "");
        return jsonObject;
    }
}
